<?php

namespace App\Http\Controllers\Api;

use App\Repositories\Admin\PictureRepository;
use App\Repositories\Admin\RankRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;

class PictureController extends Controller
{
    protected $pictureRepository;
    protected $rankRepository;
    public function __construct(PictureRepository $pictureRepository)
    {
        $rankRepository = new RankRepository();
        $this->pictureRepository = $pictureRepository;
        $this->rankRepository = $rankRepository;
    }

    /**
     * 暂时退赛
     */
    public function quitMatch(Request $request){
        if($request->has('pictureId')){
            $id = $request->post('pictureId');
            $pictureToStatus = $request->post('pictureToStatus');
            $result = $this->pictureRepository->updateStatus($id,$pictureToStatus);
            if($result){
                return json_encode(['code' => 200,'msg'=>'修改成功']);
            }
        }
    }

    /**
     * 增加血量
     */
    public function addHealthPoint(Request $request){
        if($request->has('pictureId')){
            $id = $request->post('pictureId');
            $picture_health_point = $request->post('picture_health_point');
//            $pictures = $this->pictureRepository->getById($id);

            $maxBlood =  Redis::get('maxBlood');
            $addPictureBlood =  Redis::get('addPictureBlood');

//            $healthPoint = $pictures->picture_health_point + $addPictureBlood;
            $healthPoint = $picture_health_point + $addPictureBlood;

            if($healthPoint > $maxBlood)
                $healthPoint = $maxBlood;
            if($healthPoint > 0){
                $result = $this->pictureRepository->updateStatus($id,1);
            }
            $result = $this->pictureRepository->updateHealthPoint($id,$healthPoint);
            if($result){
                return json_encode(['code' => 200,'healthPoint'=>$healthPoint]);
            }
        }
    }

    /**
     * 用户上传图片
     */
    public function upload(Request $request){
//        return json_encode($request->toArray());
        if($request->has('user_id')&&$request->has('picture_thumbnail')&&$request->has('picture_category_id')){
            $userId = $request->post('user_id');
            $picture_thumbnail = $request->post('picture_thumbnail');
            $picture_category_id = $request->post('picture_category_id');
            $rank = $this->rankRepository->getByTypeRankNumberStatus(2, 1, 1);
            $picture = [
                'picture_user_id' => $userId,
                'picture_thumbnail' => $picture_thumbnail,
                'picture_category_id' => $picture_category_id,
                'picture_uprank_condition' => $rank->rank_condition,
                'picture_uprank_type' => 1,
                'picture_status' => 1,
                'picture_health_point' => Redis::get('pictureBloode'),
            ];
            $picture = $this->pictureRepository->createPicture($picture);
            if ($picture) {
                if (Redis::exists('pictureNumber')) {
                    $pictureNumber = Redis::get('pictureNumber');
                    $pictureNumber++;
                    Redis::set('pictureNumber', $pictureNumber);
                } else {
                    Redis::set('pictureNumber', 1);
                }
                //今日新增图片
                if (Redis::exists('dayPictureNumber')) {
                    $dayPictureNumber = Redis::get('dayPictureNumber');
                    $dayPictureNumber++;
                    Redis::set('dayPictureNumber', $dayPictureNumber);
                } else {
                    Redis::set('dayPictureNumber', 1);
                }
                //今日各分组新增图片
                if (Redis::exists('dayPictureCategoryId_'.$picture_category_id)) {
                    $dayPictureCategoryIdNumber = Redis::get('dayPictureCategoryId_'.$picture_category_id);
                    $dayPictureCategoryIdNumber++;
                    Redis::set('dayPictureCategoryId_'.$picture_category_id, $dayPictureCategoryIdNumber);
                } else {
                    Redis::set('dayPictureCategoryId_'.$picture_category_id, 1);
                }
                return json_encode(true);
            } else {
                return json_encode(false);
            }

        }else{
            return json_encode(['code' => 400,'msg'=>'上传文件错误']);
        }
    }

    /**
     * 获取文件后缀名
     * @param $fileName
     * @return string
     */
    public function getExeName($fileName)
    {
        $pathinfo = pathinfo($fileName);
        return strtolower($pathinfo['extension']);
    }
}
